﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Transactions;
using DataAccess;

#region VƯƠNG TRÍ LỢI
namespace BusinessLogic
{
    public class KMThuChi : MarshalByRefObject
    {
        private static SqlCommand cmd;
        private static ArrayList arr;
        private static SqlDataReader dr;
        private static SqlConnection cn;

        public KMThuChi() 
        {
            cmd = null;
            arr = null;
            dr = null;
            cn = null;
        }

        public static Entities.KMThuChi[] Select(Entities.KMThuChi km)
        {
            Entities.KMThuChi[] list = null;
            try
            {

                cn = DataAccess.Connection.OpenConnection();
                cmd = new SqlCommand(Common.Constants.KMThuChi.sp_Select_KMThuChi, cn);
                cmd.Parameters.Add(Common.Constants.KMThuChi.HanhDong, SqlDbType.NVarChar).Value = km.HanhDong;
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                arr = new ArrayList();
                while (dr.Read())
                {
                    Entities.KMThuChi row = new Entities.KMThuChi();
                    row.ThuChiID = int.Parse(dr[Common.Constants.KMThuChi.ThuChiID].ToString());
                    row.MaKhoanMuc = dr[Common.Constants.KMThuChi.MaKhoanMuc].ToString();
                    row.TenKhoanMuc = dr[Common.Constants.KMThuChi.TenKhoanMuc].ToString();
                    row.LoaiKM = Boolean.Parse(dr[Common.Constants.KMThuChi.LoaiKM].ToString());
                    if (Boolean.Parse(dr[Common.Constants.KMThuChi.LoaiKM].ToString()) == false)
                    {
                        row.TenLoaiKM = "Khoản mục Thu";
                    }
                    else
                    {
                        row.TenLoaiKM = "Khoản Mục Chi";
                    }
                    row.DoiTuong = dr[Common.Constants.KMThuChi.DoiTuong].ToString();
                    row.NoTK = dr[Common.Constants.KMThuChi.NoTK].ToString();
                    row.CoTK = dr[Common.Constants.KMThuChi.CoTK].ToString();
                    row.GhiChu = dr[Common.Constants.KMThuChi.GhiChu].ToString();
                    row.Deleted = Boolean.Parse(dr[Common.Constants.KMThuChi.Deleted].ToString());
                    arr.Add(row);
                }
                int n = arr.Count;
                if (n == 0) { list = null; }
                list = new Entities.KMThuChi[n];
                for (int i = 0; i < n; i++)
                {
                    list[i] = (Entities.KMThuChi)arr[i];
                }
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); list = null; }
            finally
            {
                cmd.Connection.Dispose();
                cn.Close();
                
            }
            return list;
        }

        public static int sp_InsertUpdate(Entities.KMThuChi km)
        {
            TransactionScope ts = null;
            int rt = 0;
            try
            {
                using (ts = new TransactionScope(TransactionScopeOption.Required))
                {
                    SqlConnection cn = DataAccess.Connection.OpenConnection();
                    SqlCommand cmd = new SqlCommand(Common.Constants.KMThuChi.sp_InsertUpdate_KMThuChi, cn);
                    cmd.Parameters.Add(Common.Constants.KMThuChi.HanhDong, SqlDbType.NVarChar).Value = km.HanhDong;
                    cmd.Parameters.Add(Common.Constants.KMThuChi.ThuChiID, SqlDbType.Int).Value = km.ThuChiID;
                    cmd.Parameters.Add(Common.Constants.KMThuChi.MaKhoanMuc, SqlDbType.VarChar).Value = km.MaKhoanMuc;
                    cmd.Parameters.Add(Common.Constants.KMThuChi.TenKhoanMuc, SqlDbType.NVarChar).Value = km.TenKhoanMuc;
                    cmd.Parameters.Add(Common.Constants.KMThuChi.LoaiKM, SqlDbType.Bit).Value = km.LoaiKM;
                    cmd.Parameters.Add(Common.Constants.KMThuChi.DoiTuong, SqlDbType.NVarChar).Value = km.DoiTuong;
                    cmd.Parameters.Add(Common.Constants.KMThuChi.NoTK, SqlDbType.VarChar).Value = km.NoTK;
                    cmd.Parameters.Add(Common.Constants.KMThuChi.CoTK, SqlDbType.VarChar).Value = km.CoTK;
                    cmd.Parameters.Add(Common.Constants.KMThuChi.GhiChu, SqlDbType.NVarChar).Value = km.GhiChu;
                    cmd.Parameters.Add(Common.Constants.KMThuChi.Deleted, SqlDbType.Bit).Value = km.Deleted;
                    rt = cmd.ExecuteNonQuery();

                    ts.Complete();
                }
            }
            catch (Exception ex)
            {
                ts.Dispose();
                string s = ex.Message.ToString(); rt = 0; }
            return rt;
        }

        public static int Delete(Entities.KMThuChi km)
        {
            int rt = 0;
            try
            {

                SqlConnection cn = DataAccess.Connection.OpenConnection(); ;
                SqlCommand cmd = new SqlCommand(Common.Constants.KMThuChi.sp_Delete_KMThuChi, cn);
                cmd.Parameters.Add(Common.Constants.KMThuChi.ThuChiID, SqlDbType.Int).Value = km.ThuChiID;
                rt = cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); rt = 0; }
            return rt;
        }
    }
}
#endregion
